package com.example.texted.cyclicbarrier;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Worker implements Runnable {
    //关卡
    private final CyclicBarrier cb;

    public Worker(CyclicBarrier cb) {
        this.cb = cb;
    }


    @Override
    public void run() {
        try {
            Thread.sleep(new Random().nextInt(10000));
            System.out.println(Thread.currentThread().getName() + "到达地点");
            //线程在这里等待其他任务到达此关卡 再开始执行
            cb.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
